<?php

namespace common\models;

use Yii;
use common\widgets\webuploader\UploaderBehavior;

/**
 * This is the model class for table "link".
 *
 * @property integer $id
 * @property string $name
 * @property string $url
 * @property string $logo
 * @property integer $create_at
 * @property integer $update_at
 * @property integer $order_num
 */
class Link extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'link';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['name', 'url', 'order_num'], 'required'],
            [['create_at', 'update_at', 'order_num'], 'integer'],
            [['name', 'url', 'logo'], 'string', 'max' => 255],
            [['url'], 'url'],
        ];
    }
    
    public function behaviors()
    {
        return [
            [
                'class' => UploaderBehavior::className(),
                'attribute' => 'logo',
            ],
        ];
    }
    
    public function beforeSave($insert){
        
        if(parent::beforeSave($insert)){
            
            if($this->isNewRecord){
                $this->create_at = time();
            }
            $this->update_at = time();
            
            return true;
        }else{
            return false;
        }
        
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'name' => '链接名称',
            'url' => '链接地址',
            'logo' => 'LOGO',
            'create_at' => '创建时间',
            'update_at' => '更新时间',
            'order_num' => '排序值',
        ];
    }
}
